package com.example.administrator.damaiwang.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.damaiwang.R;
import com.example.administrator.damaiwang.adapter.MyAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_yanchu extends Fragment {
    List<Fragment> fragments=new ArrayList<>();
    private ViewPager vp;
    private TabLayout tab;
    private String[] titles;
    private String[] url;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment_yanchu, container, false);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tab = (TabLayout) view.findViewById(R.id.tabs);
        vp = (ViewPager) view.findViewById(R.id.vp);
        MyAdapter adapter =new MyAdapter(getChildFragmentManager(),fragments);
        url=new String[]{"http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=0&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=1&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=2&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=3&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=4&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=5&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=6&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=7&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=8&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=100&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852",
                "http://mapi.damai.cn/ProjLst.aspx?source=10101&cc=0&ps=20&mc=200&ot=0&v=0&appType=1&osType=2&p=1&version=50609&channel_from=xiaomi_market&cityId=852"};
        titles = new String[]{"全部分类","演唱会","音乐会","话剧歌剧","舞蹈芭蕾","曲苑杂坛","体育比赛","休闲度假","周边商品","儿童亲子","动漫"};
        for (int i = 0; i < url.length; i++) {
            fragments.add(new Fragment_all(url[i]));
        }
        tab.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < titles.length ; i++) {
            tab.addTab(tab.newTab().setText(titles[i]));
        }
        vp.setOffscreenPageLimit(fragments.size());
        vp.setAdapter(adapter);


//        tab.setupWithViewPager(vp);
//        tab.setTabsFromPagerAdapter(adapter);
        tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                vp.setCurrentItem(tab.getPosition());
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                tab.setScrollPosition(position,positionOffset,true);
            }

            @Override
            public void onPageSelected(int position) {
//                tab.setScrollPosition(position,position,true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
